const getPermission = () => {
    getPermission().then(res => {
        let menuListTemp = res.data.data.menuList;
        let menuList = menuListTemp.map(item => item.menuCode);
        let permissionList = [];
        menuListTemp.forEach(item => {
            item.permissionList.forEach(permission => {
                permissionList.push(permission.permission);
            });
        });
        localStorage.setItem('permissionList', JSON.stringify(permissionList));
        localStorage.setItem('menuList', JSON.stringify(menuList));
    });
};

const hasMenu = menuCode => {
    let menuSet = new Set(JSON.parse(localStorage.getItem('menuList')));
    return menuSet.has(menuCode);
};

const hasPermission = permission => {
    let permissionSet = new Set(JSON.parse(localStorage.getItem('permissionList')));

    return permissionSet.has(permission);
};

const permissionListContain = permissionList => {
    let permissionSet = new Set(JSON.parse(localStorage.getItem('permissionList')));
    for (let i = 0; i < permissionList.length; i++) {
        let permission = permissionList[i];
        if (permissionSet.has(permission)) {
            return true;
        }
    }
    return false;
};
export { hasMenu, hasPermission, permissionListContain, getPermission };
